<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="币种标识" prop="symbol">
        <el-input
          v-model="queryParams.symbol"
          placeholder="请输入币种标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主币种标识" prop="mainSymbol">
        <el-input
          v-model="queryParams.mainSymbol"
          placeholder="请输入主币种标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="状态" size="small" :clearable="true">
            <el-option
              v-for="(v, k) in statusMap"
              :key="k"
              :label="v"
              :value="Number(k)"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['aipay:coin:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="币种简称" align="center" prop="name"/>
      <el-table-column label="币种全称" align="center" prop="fullName" />
      <el-table-column label="币种标识" align="center" prop="symbol" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ typeMap[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column label="币种精度" align="center" prop="decimals" />
      <el-table-column label="币种图片" align="center" prop="logo">
        <template slot-scope="scope">
          <el-image style="width: 40px; height: 40px" :src="scope.row.logo" ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="主币种标识" align="center" prop="mainSymbol" />
      <el-table-column label="合约地址" align="center" prop="contractAddr">
        <template slot-scope="scope">
          {{ scope.row.contractAddr }}
        </template>
      </el-table-column>
      <el-table-column label="GasLimit" align="center" prop="gasLimit" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ statusMap[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aipay:coin:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aipay:coin:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="币种简称" prop="name">
          <el-input v-model="form.name" placeholder="请输入币种简称" />
        </el-form-item>
        <el-form-item label="币种全称" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入币种全称" />
        </el-form-item>
        <el-form-item label="币种标识" prop="symbol">
          <el-input v-model="form.symbol" placeholder="请输入币种标识" />
        </el-form-item>
        <el-form-item label="币种类型" prop="type">
          <el-select v-model="form.type" placeholder="币种类型">
            <el-option
              v-for="(v, k) in typeMap"
              :key="k"
              :label="v"
              :value="Number(k)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="币种图片" prop="logo">
          <el-input v-model="form.logo" placeholder="请输入币种图片" />
        </el-form-item>
        <el-form-item label="主币种ID" prop="mainCoinId">
          <el-input v-model="form.mainCoinId" placeholder="请输入主币种ID" />
        </el-form-item>
        <el-form-item label="主币种标识" prop="mainSymbol">
          <el-input v-model="form.mainSymbol" placeholder="请输入主币种标识" />
        </el-form-item>
        <el-form-item label="币种精度" prop="decimals">
          <el-input v-model="form.decimals" placeholder="请输入币种精度" />
        </el-form-item>
        <el-form-item label="合约地址" prop="contractAddr">
          <el-input v-model="form.contractAddr" placeholder="请输入合约地址" />
        </el-form-item>
        <el-form-item label="GasLimit" prop="gasLimit">
          <el-input v-model="form.gasLimit" placeholder="请输入GasLimit" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态" size="small">
            <el-option
              v-for="(v, k) in statusMap"
              :key="k"
              :label="v"
              :value="Number(k)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoin, getCoin, addCoin, updateCoin, delCoin  } from "@/api/aipay/coin";

export default {
  name: "Resource",
  dicts: ['sys_bool_status', 'sys_open_close'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      postList: [],
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName: "public",
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        status: undefined,
        id: undefined,
        username: undefined,
        mobile: undefined
      },
      typeMap: {
        0: '主币',
        1: '代币'
      },
      statusMap: {
        0: '关闭',
        1: '开启'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCoin(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加币种";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getCoin(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改币种";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCoin(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoin(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCoin(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
